package org.sensornet2010.prototype;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.io.Connector;
import javax.microedition.io.ContentConnection;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class TestHttpGet extends MIDlet implements CommandListener {

	private Command exitCommand; // The exit command
	private Command getCommand;

	private Display display; // The display for this MIDlet
	private StringItem debugInfo;
	private TextField url, parameter;


	public TestHttpGet() {
		this.exitCommand = new Command("Exit", Command.EXIT, 1);
		this.getCommand = new Command("GET", Command.SCREEN, 1);
		this.debugInfo = new StringItem("DEBUGINFO", "debug...");

		this.display = Display.getDisplay(this);
		this.url = new TextField("URL", "http://eprints.fatronik.com", 100, 0);
		this.url.setString("http://www.snee.com/xml/crud/posttest.cgi");
		this.parameter = new TextField("PARAMETER", "", 100, 0);

	}

	// Destroy must cleanup everything not handled
	// by the garbage collector.
	// In this case there is nothing to cleanup.
	protected void destroyApp(boolean unconditional) {
		// throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	// Pause is a no-op because there are no background
	// activities or record stores to be closed.
	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
		Form f = new Form("TestHttp Midlet");
		f.append(this.url);
		f.append(this.debugInfo);
		f.append(this.parameter);
		f.addCommand(this.exitCommand);
		f.addCommand(this.getCommand);
		f.setCommandListener(this);
		display.setCurrent(f);
	}

	// Respond to commands. Here we are only implementing
	// the exit command. In the exit command, cleanup and
	// notify that the MIDlet has been destroyed.
	public void commandAction(Command c, Displayable d) {
		if (c == exitCommand) {
			destroyApp(false);
			notifyDestroyed();
		} else if (c == this.getCommand) {
			this.doGET();
		} 
	}

/*	private void doHTTP() {

		final String url = this.url.getString();

		Thread t = new Thread(new Runnable() {
			public void run() {

				try {
					HttpConnection hc = null;
					InputStream in = null;

					try {
						hc = (HttpConnection) Connector.open(url);
						in = hc.openInputStream();

						int contentLength = (int) hc.getLength();
						byte[] raw = new byte[contentLength];
						int length = in.read(raw);

						in.close();
						hc.close();

						// Show the response to the user.
						String s = new String(raw, 0, length);
						debugInfo.setText(s);
					} catch (IOException ioe) {
						debugInfo.setText("IOE:" + ioe.toString());
						ioe.printStackTrace();
					}
				} catch (Exception e) {
					debugInfo.setText("E:" + e.toString());
					e.printStackTrace();
				}
			}
		});
		this.debugInfo.setText("http done");
		t.start();
	}
*/
	private void doGET() {

		final String url = this.url.getString();
		Thread t = new Thread(new Runnable() {
			public void run() {
				try {
					// String content = getViaHttpConnection(url);
					// String content = connect(url);
					String content = getViaContentConnection(url);
					debugInfo.setText(content);
				} catch (Exception e) {
					e.printStackTrace();
					debugInfo.setText(e.getMessage());
				}

			}
		});
		t.start();
		this.debugInfo.setText("doGET");
	}

	private String getViaContentConnection(String url) throws IOException {
		ContentConnection c = null;
		DataInputStream dis = null;
		String s = "";

		try {
			c = (ContentConnection) Connector.open(url);
			int len = (int) c.getLength();
			dis = c.openDataInputStream();
			if (len > 0) {
				byte[] data = new byte[len];
				dis.readFully(data);
				s = new String(data, 0, len);
			} else {
				int ch;
				while ((ch = dis.read()) != -1) {
					byte[] data = new byte[1];
					data[0] = (byte) ch;
					s += new String(data, 0, 1);
				}
			}
		} finally {
			if (dis != null)
				dis.close();
			if (c != null)
				c.close();
		}
		return s;
	}
}
